package com.asa.jihe.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.jihe.utils.Line;
import com.asa.jihe.utils.Point;
import com.asa.ui.spirit.Circle;
/**
 * 这个不怎么用java的控件，都是自己画在JPanel上的
 * @author asa
 *
 */
public class JiHeFrame extends JFrame{
	
	public JiHeFrame(String title,int canvasWidth,int canvasHeight) {
		super(title);
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		
		JiHeCanvas canvas = new JiHeCanvas();

		this.setContentPane(canvas);
		pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	private int canvasWidth;
	private int canvasHeight;
	private Circle[] circles;
	private List<Point> pionts;
	private List<Line> lines;
	private String operation = "";
	private String operationchange = "";

	
	public void render(Circle[] circles,List<Point> pionts,List<Line> lines,String operation,String operationchange) {
		
		this.circles = circles;
		this.pionts = pionts;
		this.lines = lines;
		this.operation = operation;
		this.operationchange = operationchange;

		this.repaint();
	}
	
	private class JiHeCanvas extends JPanel{
		
		@Override
		public void paintComponent(Graphics g) {
			
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			JiHeVisHelper.setRenderingHints(g2d);
			
			for (int i = 0; i < pionts.size(); i++) {
				JiHeVisHelper.drawPoint(g2d, pionts.get(i));
			}
			
			for (int i = 0; i < lines.size(); i++) {
				Line line = lines.get(i);
				Point spoint = line.spoint;
				Point epoint = line.epoint;
				int x1 = (int) spoint.x;
				int y1 = (int) spoint.y;
				int x2 = (int) epoint.x;
				int y2 = (int) epoint.y;
				
				JiHeVisHelper.drawLine(g2d, x1, y1, x2, y2);
				
			}
			
			JiHeVisHelper.drawString(g2d, operation, canvasWidth-100, canvasHeight);
			
			JiHeVisHelper.drawString(g2d, operationchange, 100, canvasHeight);

			
			
		}
		
		
		@Override
		public Dimension getPreferredSize() {
			
			return new Dimension(canvasWidth, canvasHeight);
		}
		
	}
	
	
	
}
